package com.xfhy.selectcity.select;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.ludashi.framework.adapter.BaseQuickAdapter;
import com.ludashi.framework.base.BaseFrameActivity;
import com.xfhy.selectcity.R;
import com.xfhy.selectcity.adapter.SelectAreaAdapter;
import com.xfhy.selectcity.adapter.SelectCityAdapter;
import com.xfhy.selectcity.data.Area;
import com.xfhy.selectcity.select.viewmodel.SelectAreaViewModel;

import java.util.ArrayList;
import java.util.List;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 选则区县
 */
public class SelectAreaActivity extends BaseFrameActivity implements BaseQuickAdapter.OnItemClickListener {

    private final static String CITY_NAME_KEY = "city_name";

    private RecyclerView mRvSelectArea;
    private SelectAreaViewModel mSelectAreaViewModel;
    private List<Area> mAreaItems = new ArrayList<>();
    private SelectAreaAdapter mSelectAreaAdapter;

    public static void startSelectAreaActivity(Context context, String cityName) {
        Intent intent = new Intent(context, SelectAreaActivity.class);
        intent.putExtra(CITY_NAME_KEY, cityName);
        context.startActivity(intent);
    }

    @Override
    protected void onSafeCreate(Bundle savedInstanceState) {
        super.onSafeCreate(savedInstanceState);
        setContentView(R.layout.activity_select_area);

        initView();
        initData();
    }

    private void initView() {
        mRvSelectArea = findViewById(R.id.rv_select_area);

        mSelectAreaAdapter = new SelectAreaAdapter(null);
        mRvSelectArea.setAdapter(mSelectAreaAdapter);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        mRvSelectArea.setLayoutManager(gridLayoutManager);
    }

    private void initData() {
        mSelectAreaViewModel = new ViewModelProvider(this).get(SelectAreaViewModel.class);
        mSelectAreaViewModel.queryAreasByCity(getIntent().getStringExtra(CITY_NAME_KEY)).observe(this, new Observer<List<Area>>() {
            @Override
            public void onChanged(List<Area> areas) {
                mAreaItems = areas;
                mSelectAreaAdapter.replaceData(mAreaItems);
            }
        });
        mSelectAreaAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        Area area = mAreaItems.get(position);
    }
}